Vue.component('time-check',
	{
				template: "<div><div id='segmentedControl' class='mui-segmented-control mui-segmented-control-inverted mui-segmented-control-primary' style='position: fixed;top:0;z-index: 9999'>\
					<a class='mui-control-item' :class='{muiActive:actnum*1===0}' index='0'>按日</a>\
					<a class='mui-control-item' :class='{muiActive:actnum*1===1}' index='1'>按周</a>\
					<a class='mui-control-item' :class='{muiActive:actnum*1===2}' index='2'>按月</a>\
				</div>\
				<div class='time-choose' style='clear:both;margin-top: 0.5rem'>\
					<div class='befor-month' @click='reduceTime()'  style='position: fixed;top:1rem;z-index: 99999' :style='{top:top}'>\
						<img src='../images/left.png' alt=''>\
						<span>上一{{actnum*1===0?'日':actnum*1===1?'周':'月'}}</span>\
					</div>\
					<div class='time-range'>\
						 <p v-show='actnum !==0' class='positionFixed' :class='{coinClass:type===2||type===3}' >{{new Date(fromdate).format('yyyy-MM-dd')}} 至 {{new Date(todate).format('yyyy-MM-dd')}}  <span v-show='actnum ===1'>({{week2}})</span> </p>\
						 <p v-show='actnum ===0' class='positionFixed' :class='{coinClass:type===2||type===3}' >{{new Date(fromdate).format('yyyy-MM-dd')}}({{week}})</p>\
						<div v-show='type===1' class='moneyDiv marginRight'>\
								<p style='font-size: 0.22rem'>总销售额</p>\
								<p style='margin-top: 0.1rem'> <strong>{{totalnum}}</strong> </p>\
						</div>\
						<div v-show='type===1'  class='moneyDiv marginLeft'>\
						 		<p style='font-size: 0.22rem' >门店实收</p>\
						 		<p style='margin-top: 0.1rem'><strong>{{realnum}}</strong></p>\
						 </div>\
						<div v-show='type===2' class='coin-total-info flex' style='width: 60%;margin: 0 auto;z-index: 999;position: relative'>\
							<div class='flex-item coin-info-item'>\
								<p style='font-size: 0.22rem'>{{names.leftName}}</p>\
								<p style='margin-top: 0.1rem'> <strong>{{leftdata}}</strong> <span style='font-size: 0.22rem'>{{unitLeft}}</span>  </p>\
							</div>\
							<div class='flex-item coin-info-item' v-show='names.rightName '>\
								<p style='font-size: 0.22rem'>{{names.rightName}}</p>\
								<p style='margin-top: 0.1rem'> <strong>{{rightdata}}</strong> <span style='font-size: 0.22rem' >{{unitRight}}</span></p>\
							</div>\
						</div>\
					</div>\
					<div class='next-month' @click='increaseTime()'  style='position: fixed;z-index: 99999' :style='{top:top}'>\
						<span>下一{{actnum*1===0?'日':actnum*1===1?'周':'月'}}</span>\
						<img src='../images/right.png' alt=''>\
					</div></div>\
				</div>",
				data: function() {
					return {
						dateBase: 1,
						date: '',
						startTime: '',
						endTime: '',
						// isShow:1,   // 0 只显示当天
					}
				},
				props:{
					names:{
						default:function(){
							return {
								leftName:'',
								rightName:''
							}
						}
					},
					totalnum:{
						default:0
					},
          realnum:{
            default:0
					},
					leftdata:{
						default:0
					},
					rightdata:{
						default:0
					},
					actnum:{
						default:0
					},
					fromdate:{
						default:'--'
					},
					todate:{
						default:'--'
					},
					type:{
						default:1
					},
          dateType:{
					},
          top:{},
          leftunit:{
            default:''
					},  //单位
          rightunit:{
            default:''
					},  //单位



				},
				created: function() {
					this.initTime()
				},
        computed:{
          week:function(){
            var week=['星期天','星期一','星期二','星期三','星期四','星期五','星期六'];
            var time = new Date(this.fromdate).getDay();
            return week[time]
          },
					week2:function(){
            // var fullYear=(this.endTime+'').replace(/^(\d{4})(\d{2})(\d{2})$/, "$1");
						var data='';
						if(this.actnum ===1){
              var endTime=new Date(this.fromdate);
              var dayWeek=endTime.getDay();
              if(dayWeek ===0){dayWeek=7}
              var year=endTime.getFullYear();
              var firstDay = new Date(endTime.getFullYear(),0, 1);
              var dayOfWeek = firstDay.getDay();
              if (dayOfWeek ===0) {
                dayOfWeek=7;
              }
              var weekNum=''
							if(new Date(firstDay).getDay() !==1){
                 weekNum =parseInt( ((endTime.getTime()-firstDay.getTime())/(24*60*60*1000)+dayOfWeek-dayWeek)/7);
							}else{
                 weekNum =parseInt( ((endTime.getTime()-firstDay.getTime())/(24*60*60*1000)+dayOfWeek-dayWeek)/7)+ 1;
							}

              data='第'+ weekNum+'周'
						}
            return data;
					},
          unitLeft:function(){
            return  this.leftdata !=='--' ? this.leftunit : ''
					},
					unitRight:function(){
            return  this.rightdata !=='--' ? this.rightunit : ''
					}
        },
				mounted:function(){
					var vm = this;
					mui('#segmentedControl').on('tap','.mui-control-item',function(){
						var index = this.getAttribute('index')*1;
            vm.$parent.dateType=index;
						vm.changeDatabase(index);
					})
				},
				methods: {
					initTime: function() {
						this.date = new Date();
						this.startTime=new Date(this.date).format('yyyy-MM-dd')
						this.endTime=new Date(this.date).format('yyyy-MM-dd')
            this.$parent.dateType=0;
					},
					changeDatabase: function(type) {
						var vm = this;
						this.$parent.actnum =  type*1
						switch(type) {
							case 0:
								this.date = new Date()
                this.startTime = new Date(this.date).format('yyyy-MM-dd')
                this.endTime = new Date(this.date).format('yyyy-MM-dd ')
								break;
							case 1:
								this.date = new Date();
								var weekday = this.date.getDay();//0:周日，1-6：周一到周六
								if(weekday===0){weekday=7};
								var startTime=new Date().getTime() - (24*60*60*1000)*(weekday-1);
								this.startTime=new Date(startTime).format('yyyy-MM-dd')
								this.endTime = new Date(this.date).format('yyyy-MM-dd')
								break;
							case 2:
								this.date = new Date()
								this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
								this.endTime = new Date(this.date).format('yyyy-MM-dd')
								break;
						}
						this.$parent.toDate = this.endTime
						this.$parent.fromDate = this.startTime
					},
					//					      减小时间
					reduceTime: function() {
            if(this.todate){
              this.date =(new Date(this.todate)).getTime()
            }
						switch(this.actnum) {
							case 0:
								this.date = this.date - 86400000
                this.startTime = new Date(this.date).format('yyyy-MM-dd')
                this.endTime = new Date(this.date).format('yyyy-MM-dd')
								break;
							case 1:
								var weekday = new Date(this.date).getDay();//0:周日，1-6：周一到周六
                if(weekday === 0) weekday = 7;
								this.date = this.date - weekday * 86400000
								this.startTime = new Date(this.date - 6 * 86400000).format('yyyy-MM-dd')
								this.endTime = new Date(this.date).format('yyyy-MM-dd')
								break;
							case 2:
								this.date = this.date - 86400000 * new Date(this.date).format('dd')
								this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
								this.endTime = new Date(this.date).format('yyyy-MM-dd')
								break;
						}
						this.$parent.toDate = this.endTime
						this.$parent.fromDate  = this.startTime
						
					},
					//      增加时间
					increaseTime: function() {
						if(this.todate){
							this.date =(new Date(this.todate)).getTime()
						}
						switch(this.actnum) {
							case 0:
                  if(new Date(this.date + 86400000).format('yyyyMMdd')*1 === new Date().format('yyyyMMdd')*1) {
                    this.date = new Date()
                    this.startTime = new Date(this.date).format('yyyy-MM-dd')
                    this.endTime = new Date(this.date).format('yyyy-MM-dd ')
                  } else if(new Date(this.date + 86400000).format('yyyyMMdd')*1 < new Date().format('yyyyMMdd')*1) {
                    this.date = this.date + 86400000
                    this.startTime = new Date(this.date).format('yyyy-MM-dd')
                    this.endTime = new Date(this.date).format('yyyy-MM-dd')
                  }
								break;
							case 1:
								var weekday = new Date().getDay();//0:周日，1-6：周一到周六
                if(weekday === 0) weekday = 7;
                if(new Date(this.date+weekday*86400000).format('yyyyMMdd')*1  === new Date().format('yyyyMMdd')*1 ) {
									this.date = new Date();
                  var startTime=new Date().getTime() - (86400000)*(weekday-1);
                  this.startTime=new Date(startTime).format('yyyy-MM-dd')
									this.endTime = new Date(this.date).format('yyyy-MM-dd')
								} else if(new Date(this.date+86400000).format('yyyyMMdd')*1  < new Date().format('yyyyMMdd')*1 ) {
									this.startTime = new Date(this.date+86400000).format('yyyy-MM-dd')
									this.endTime = new Date(this.date+ 7 * 86400000).format('yyyy-MM-dd')
									this.date = this.date+ 7 * 86400000;
								}
								break;
							case 2:
                if(new Date(this.date).format('yyyy-MM') === new Date().format('yyyy-MM')) {
                  this.date = new Date()
                  this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
                  this.endTime = new Date().format('yyyy-MM-dd')
                }
								if(new Date(this.date).format('yyyy') < new Date().format('yyyy') || [new Date(this.date).format('yyyy') === new Date().format('yyyy') && (new Date(this.date).format('MM') * 1 + 1) < new Date().format('MM') * 1]) {
									var array = new Date(this.date).format('yyyy-MM').split('-')
									var nextMonthday = 32 - new Date(array[0] * 1, array[1] * 1, 32).getDate()
									this.date = this.date + 86400000 * nextMonthday
									this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
									this.endTime = new Date(this.date).format('yyyy-MM-dd')
								}
								break;
						}
						this.$parent.toDate = this.endTime
						this.$parent.fromDate  = this.startTime
					}
				}
			})